﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;
using Utilities;

namespace BusinessLogicLayer
{
    public class PhatManager
    {

        public ActionResultAsList<Phat> LayDS()
        {
            var ar = new ActionResultAsList<Phat>();
            try
            {
                using (var db = new ThuVienDbContext())
                {
                    ar.ObjectList = db.Phats.ToList();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ar.AddException(ex);
            }
            return ar;
        }
        public ActionResult Them(Phat phat)
        {
            var ar = new ActionResult();
            // use transaction
            using (var transaction = new TransactionScope())
            {
                using (var db = new ThuVienDbContext())
                {
                    try
                    {
                        db.Phats.Add(phat);
                        db.SaveChanges();
                        // Update state of Sach
                        if (phat.LoaiPhat == "Mất sách")
                        {
                            var re = new SachManager().CapNhatTrangThaiSach(phat.MaSach, "Mất");
                            if (re.Success)
                                transaction.Complete();
                            else ar.Exceptions.AddRange(re.Exceptions);
                        }
                        else transaction.Complete();
                    }
                    catch (Exception ex)
                    {
                        Logger.Write(ex);
                        ar.AddException(ex);
                    }
                }
            }
            return ar;
        }
        public ActionResult Xoa(int maPhat)
        {
            var ar = new ActionResult();
            // use transaction
            using (var transaction = new TransactionScope())
            {
                using (var db = new ThuVienDbContext())
                {
                    try
                    {
                        var phat = db.Phats.Single(ng => ng.MaPhat == maPhat);
                        // save book need to update
                        string idBook = phat.MaSach;
                        db.Phats.Remove(phat);
                        // Update state of Sach
                        if (phat.LoaiPhat == "Mất sách")
                        {
                            var sach = db.Saches.Single(s => s.MaSach == idBook);
                            sach.TrangThai = "Sẵn sàng";
                        }
                        db.SaveChanges();
                        transaction.Complete();
                    }
                    catch (Exception ex)
                    {
                        Logger.Write(ex);
                        ar.AddException(ex);
                    }
                }
            }
            return ar;
        }
        public ActionResultAsList<Phat> TimTheoMaDG(string maDG)
        {
            var ar = new ActionResultAsList<Phat>();
            try
            {
                using (var db = new ThuVienDbContext())
                {
                    var list = db.Phats.Where(p => p.MaDG.Contains(maDG));
                    ar.ObjectList = list.ToList();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ar.AddException(ex);
            }
            return ar;
        }
        public ActionResultAsList<Phat> TimTheoMaSach(string maSach)
        {
            var ar = new ActionResultAsList<Phat>();
            try
            {
                using (var db = new ThuVienDbContext())
                {
                    var list = db.Phats.Where(p => p.MaSach.Contains(maSach));
                    ar.ObjectList = list.ToList();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ar.AddException(ex);
            }
            return ar;
        }
        public ValidateResult KiemTraDGBiPhat(string maDG)
        {
            ValidateResult vr = new ValidateResult();
            try
            {
                using (var db = new ThuVienDbContext())
                {
                    var biPhat = db.Phats.Where(p => p.MaDG == maDG && p.HanPhat >= DateTime.Now);
                    if (biPhat.Any())
                        vr.AddMessage("Độc giả đang bị phạt");
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                vr.AddMessage(ex.Message);
            }
            return vr;
        }
        public ValidateResult KiemTraRangBuocSach(string maSach)
        {
            ValidateResult vr = new ValidateResult();
            try
            {
                using (var db = new ThuVienDbContext())
                {
                    var biRB = db.Phats.Where(p => p.MaSach == maSach);
                    if (biRB.Any())
                        vr.AddMessage("Bị ràng buộc sách");
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                vr.AddMessage(ex.Message);
            }
            return vr;
        }
        public ValidateResult KiemTraRangBuocDG(string maDG)
        {
            ValidateResult vr = new ValidateResult();
            try
            {
                using (var db = new ThuVienDbContext())
                {
                    var biRB = db.Phats.Where(p => p.MaDG == maDG);
                    if (biRB.Any())
                        vr.AddMessage("Bị ràng buộc độc giả");
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                vr.AddMessage(ex.Message);
            }
            return vr;
        }
        public ActionResultAsList<Sach> TimKiemSachMat(int thang, int nam)
        {
            ActionResultAsList<Sach> ar = new ActionResultAsList<Sach>();
            try
            {
                using (var db = new ThuVienDbContext())
                {
                    db.Configuration.ProxyCreationEnabled = false;
                    var re = db.Phats.Include("Sach").Include("Sach.NXB1").Include("Sach.TheLoai1").Where(p => p.NgayPhat.Month == thang
                                                            && p.NgayPhat.Year == nam
                                                            && p.LoaiPhat == "Mất sách");
                    List<Sach> list = new List<Sach>();
                    foreach (var item in re)
                        list.Add(item.Sach);
                    ar.ObjectList = list;
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ar.AddException(ex);
            }
            return ar;
        }
    }
}

